import asyncio

import requests as requests
from pyppeteer import launch


async def main():
    proxy = requests.get('http://proxy.jiayincredit.net/random').text  # 代理服务器地址和端口
    print(f'proxy:{proxy}')
    browser = await launch({'args': ['--proxy-server=http://' + proxy], 'headless': False})
    page = await browser.newPage()
    await page.goto('https://car.autohome.com.cn/config/series/6336.html#pvareaid=3454437')
    # await page.waitForNavigation()
    await asyncio.sleep(5)
    title = await page.evaluate('document.title')
    print(f'title:{title}')
    await page.waitForSelector('#config_nav')
    elements = await page.xpath('//*[@id="config_nav"]/table/tbody/tr[1]/td[1]/div[2]/div/a/text()')

    values = []
    for element in elements:
        print(f'element:{element}')
        value = await element.executionContext.evaluate('(element) => element')  # 或者使用其他属性如 element.innerHTML, element.value 等
        values.append(value)
    print(values)  # 输出所有获取到的值
    await page.screenshot({'path': 'example.png'})
    await browser.close()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
